package sy.util;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JGpush {
	
	/****************************物业端*******************************/
	private static final String appKeyWY = "f7da6d8af1224999462d44c7";
	private static final String masterSecretWY = "02a3afe0043ff5501fc6dd66";
	
	/****************************业主端*******************************/
	private static final String appKeyYZ = "3a74312a795793257ac2ddbb";
	private static final String masterSecretYZ = "c4fd29cacccfd5d6095d7e73";
	
	
	
	public static void main(String[] arg){
		Map<String, String> jgPushParm = new HashMap<String, String>();
		jgPushParm.put("msg", "推送测试");
		jgPushParm.put("key", "complaint");
		JGpush.jpushAndroidByAlias(jgPushParm, "15107962485");
		//JGpush.jpushAndroidByTag(jgPushParm, "GC");
		//JGpush.jpushIOSByTag(jgPushParm, "AGM");
	}
	/**
	 * 根据标签推送消息(适用于物业端)
	 * @Title: jpushAndroidByTag
	 * @Description: TODO
	 * @param: @param parm
	 * @param: @param pushTag     
	 * @return: void     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月30日 下午1:36:12
	 * @throws
	 */
	public static void jpushAndroidByTag(Map<String, String> parm, String pushTag) {
		JPushClient jpushClient = new JPushClient(masterSecretWY, appKeyWY);
		//推送的关键,构造一个payload 
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.android())//指定android平台的用户
				.setAudience(Audience.tag(pushTag))//你项目中的所有用户
				.setNotification(Notification.android(parm.get("msg"), "新消息", parm))
				//发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
				.setOptions(Options.newBuilder().setApnsProduction(false).build())
				//这里是指定开发环境,不用设置也没关系
				.setMessage(Message.content(parm.get("key")))//自定义信息
				.build();

		try {
			PushResult pu = jpushClient.sendPush(payload);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}    
	}

	/**
	 * (适用于物业端)
	 * @Title: jpushIOSByTag
	 * @Description: TODO
	 * @param: @param parm
	 * @param: @param pushTag     
	 * @return: void     
	 * @author:  xieyuanqiu
	 * @date: 2017年10月1日 上午11:25:35
	 * @throws
	 */
	public static  void jpushIOSByTag(Map<String, String> parm, String pushTag) {
		JPushClient jpushClient = new JPushClient(masterSecretWY, appKeyWY);
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.ios())//ios平台的用户
				.setAudience(Audience.tag(pushTag))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder()
								.setAlert(parm.get("msg"))
								//.setBadge(+1)
								.autoBadge()//解决移动端角标问题
								.setSound("happy")
								.addExtras(parm)
								.build())
								.build())
								.setOptions(Options.newBuilder().setApnsProduction(false).build())
								.setMessage(Message.newBuilder().setMsgContent(parm.get("key")).addExtras(parm).build())//自定义信息
								.build();

		try {
			PushResult pu = jpushClient.sendPush(payload);

		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}    
	}
	
	/**
	 * 根据别名推送消息（适用于业主端）
	 * @Title: jpushAndroidByAlias
	 * @Description: TODO
	 * @param: @param parm
	 * @param: @param pushAlias     
	 * @return: void     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月30日 下午1:36:34
	 * @throws
	 */
	public static void jpushAndroidByAlias(Map<String, String> parm, String pushAlias) {
		JPushClient jpushClient = new JPushClient(masterSecretYZ, appKeyYZ);
		//推送的关键,构造一个payload 
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.android())//指定android平台的用户
				.setAudience(Audience.alias(pushAlias))
				.setNotification(Notification.android(parm.get("msg"), "新消息", parm))
				.setOptions(Options.newBuilder().setApnsProduction(false).build())
				.setMessage(Message.content(parm.get("msg")))//自定义信息
				.build();

		try {
			PushResult pu = jpushClient.sendPush(payload);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}    
	}
	
	/**
	 * 适用于业主端
	 * @Title: jpushIOSByAlias
	 * @Description: TODO
	 * @param: @param parm
	 * @param: @param pushAlias     
	 * @return: void     
	 * @author:  xieyuanqiu
	 * @date: 2017年10月1日 上午11:27:20
	 * @throws
	 */
	public static  void jpushIOSByAlias(Map<String, String> parm, String pushAlias) {
		// 设置好账号的app_key和masterSecret是必须的
		//创建JPushClient
		JPushClient jpushClient = new JPushClient(masterSecretYZ, appKeyYZ);
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.ios())//ios平台的用户
				.setAudience(Audience.alias(pushAlias))//推送别名
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder()
								.setAlert(parm.get("msg"))
								.autoBadge()
								//.setBadge(+1)
								.setSound("happy")
								.addExtras(parm)
								.build())
								.build())
								.setOptions(Options.newBuilder().setApnsProduction(false).build())
								.setMessage(Message.newBuilder().setMsgContent(parm.get("key")).addExtras(parm).build())//自定义信息
								.build();

		try {
			PushResult pu = jpushClient.sendPush(payload);

		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}    
	}
}
